package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.CreateAddrActivity;
import com.linkage.jz.bankruptcy.bean.AddressInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.seapeak.base.BaseWhiteActivity;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * A login screen that offers login via email/password.
 */
public class EditManagerUserInfoActivity extends BaseWhiteActivity {
    private static final String TAG = EditManagerUserInfoActivity.class.getSimpleName();

    private int REQUEST_CODE_ADDRESS = 10001;

    private ManagerUserInfo mInfo;

    private String addressId;

    private EditText etName;
    private EditText etOrg;
    private TextView tvAccount;
    private EditText etEmail;
    private TextView tvAddr;
    private EditText etOtherphone;

    public static void start(Context context, ManagerUserInfo info) {
        Intent intent = new Intent(context, EditManagerUserInfoActivity.class);
        intent.putExtra("info", new Gson().toJson(info));
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_manageruser, "编辑资料");
        String bookJson = getIntent().getStringExtra("info");
        mInfo = new Gson().fromJson(bookJson, ManagerUserInfo.class);
//        address = (AddressInfo) getIntent().getSerializableExtra("address");
        initView();
    }

    private void initView() {
        etName = (EditText) findViewById(R.id.et_Name);
        etOrg = (EditText) findViewById(R.id.et_org);
        tvAccount = (TextView) findViewById(R.id.tv_account);
        etEmail = (EditText) findViewById(R.id.et_email);
        tvAddr = (TextView) findViewById(R.id.tv_addr);
        etOtherphone = (EditText) findViewById(R.id.et_otherphone);

        etName.setText(mInfo.getName());
        etOrg.setText(mInfo.getSubsidiaryOrgan());

//        if()
//        if(mInfo.getUser().getId().equalsIgnoreCase(BankruptcyApp.loginInfo.getId()))
//        {
//            etOrg.setText(mInfo.getSubsidiaryOrgan());
//        }
        tvAccount.setText(mInfo.getUser().getLoginname());
        etEmail.setText(mInfo.getUser().getEmail());
        etOtherphone.setText(mInfo.getOtherContactNumber());
        addressId = mInfo.getSysAddressId();



        if (mInfo.getAddress() != null)
            tvAddr.setText(mInfo.getAddress().getDetailString());

        tvAddr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditManagerUserInfoActivity.this, CreateAddrActivity.class);
                intent.putExtra(CreateAddrActivity.KEY_FROM,CreateAddrActivity.FROM_FLOW);
                startActivityForResult(intent, REQUEST_CODE_ADDRESS);
            }
        });
        addMenu(new MenuAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        }, "完成", 0));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_ADDRESS) {
//                address = (AddressInfo) data.getSerializableExtra("address");
//                tvAddr.setText(address.getDetailString());
                addressId = data.getStringExtra("id");
                tvAddr.setText(data.getStringExtra("desc"));
            }
        }
    }

    public void saveData() {
        String str_et_Name = etName.getText().toString();
        if ("".equalsIgnoreCase(str_et_Name)) {
            showToast("名称不能为空");
            return;
        }
        String str_et_org = etOrg.getText().toString();
        if ("".equalsIgnoreCase(str_et_org)) {
            showToast("所属机构不能为空");
            return;
        }

        showProgress("提交信息中...");
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            return;
        }

        if (addressId == null)
            addressId = "";

        Network.getAPIService()
                .editManagerMember(mInfo.getId(), mInfo.getSysUserId(), str_et_Name, str_et_org,
                        etEmail.getText().toString(), etOtherphone.getText().toString(), addressId, getUserToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideProgress();
                        if(json.get("code").getAsInt() == 0) {
                            showToast("修改成功");
                            finish();
                        } else
                            showToast(json.get("msg").getAsString());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideProgress();
                        showToast("修改失败");
                    }
                });
    }
}

